$(document).ready(function() {
	init();
	
});

//参数
var query = {
		page: 1,
		rows: 15,
		type: "",
		beginTime: "",
		endTime:""
};

function init(){
	query.page = 1;
	queryRanking(1);
}
function queryRanking(pageNum){
	if(undefined!=pageNum)
	{
		query.page = pageNum;
	}
	else
	{
		query.page = 1;
	}
	query.type = $("#query_type").val();
	query.beginTime = $("#param_begintime").val();
	query.endTime = $("#param_endtime").val();
	createContent();
}

function showEdit(id){
	var editPath = ctx+'/views/toRankingEdit';
	if(undefined != id){
		editPath += '?id='+id;
	}
	window.location.href = editPath;
		
}

function showRankingdetail(id){
	var editPath = ctx+'/views/showRankingdetail';
	if(undefined != id){
		editPath += '?id='+id;
	}
	window.location.href = editPath;
	
}

function convertType(type){
	var result = "";
	if(type=="power"){
		result = "九阳神功排名";
	}else if(type=="bean"){
		result = "创业豆排名";
	}else if(type=="coins"){
		result = "创业币排名";
	}
	return result;
}

function createContent(){
	var path = ctx+"/ranking/list";
	commonAjax(path,query,function(data){
		if(data.success){
			var pageStr = new Array; 
			for(var index=0;index<data.rows.length;index++) {
				var row = data.rows[index];
				pageStr.push('<tr>');
				pageStr.push('<td>'+(index+1)+'</td>');
				pageStr.push('<td><a href="#">'+row.createtime+'</a></td>');
				pageStr.push('<td>'+convertType(row.type)+'</td>');
				pageStr.push('<td class="am-hide-sm-only">'+row.num+'</td>');
				pageStr.push(' <td>');
				pageStr.push('<div class="am-btn-toolbar">');
				pageStr.push(' <div class="am-btn-group am-btn-group-xs">');
//				pageStr.push('  <button onclick="showEdit('+row.id+')" class="am-btn am-btn-default am-btn-xs am-text-firstly"><span class="am-icon-pencil-square-o"></span> 编辑</button>');
				pageStr.push('  <button onclick="showRankingdetail('+row.id+')" class="am-btn am-btn-default am-btn-xs am-text-secondary"><span class="am-icon-play"></span> 查看</button>');
				pageStr.push('  <button onclick="delRanking('+row.id+')" class="am-btn am-btn-default am-btn-xs am-text-danger "><span class="am-icon-trash-o"></span> 删除</button>');
				pageStr.push(' </div>');
				pageStr.push(' </div>');
				pageStr.push(' </td>');
				pageStr.push(' </tr>');
			}
			
			$("#pagelist").html(pageStr.join(""));
			
			var page_options = {
					id:"ranking_page",
					pageCount:data.pageCount,
					current:data.pageNumber,
					callback:"queryRanking"
			}
			pages(page_options);
		}else{
			showMsg(data.msg);
		}
	});
}

function showMsg(msg){
	$("#alert-msg").html(msg);
	$("#my-msg").modal();
}

function delRanking(rid) {
	var delPath = ctx+'/ranking/del';
	var depParam = {
		rid: rid
	};
	
	showConfirm("确认删除该条记录吗？",function(){
		commonAjax(delPath, depParam, function(data){
			if(data.success){
				queryRanking();
			}
			showMsg(data.msg);
		});
	});
}

